//
//  LandmarkList.swift
//  Landmarks
//
//  Created by Hello Cai on 2020/4/6.
//  Copyright © 2020 Hello Cai. All rights reserved.
//

import SwiftUI

struct LandmarkList: View {
    
    //@State var showFavoritesOnly = false
    @EnvironmentObject private var userData: UserData
    
    var body: some View {
        //指定单个元素
//        List{
//            LandmarkRow(landmark: landmarkData[0])
//            LandmarkRow(landmark: landmarkData[1])
//        }
        
        //导航栏
        NavigationView {
            
            //动态化列表
            List {
                //使用 $ 前缀来访问一个状态变量或者它的属性的绑定
                Toggle(isOn: $userData.showFavoritesOnly) {
                    Text("Show Favorites Only")
                }
                
                ForEach(userData.landmarks) { landmark in
                    if !self.userData.showFavoritesOnly || landmark.isFavorite {
                        //行视图包装在一个 NavigationButton 中，并把LandmarkDetail作为目标
                        NavigationLink(
                            destination: LandmarkDetail(landmark: landmark)
                                .environmentObject(self.userData)
                        ) {
                            LandmarkRow(landmark: landmark)
                        }

                    }
                }
            }
            //导航栏的标题
            .navigationBarTitle(Text("Landmarks"))
        }
    }
}

struct LandmarkList_Previews: PreviewProvider {
    static var previews: some View {
        LandmarkList()
            .environmentObject(UserData())
//        .previewDevice(PreviewDevice(rawValue: "iPhone SE"))
        
        //  \.self 作为标识符的 key path
//        ForEach(["iPhone SE", "iPhone XS Max"], id: \.self) { deviceName in
//            LandmarkList().previewDevice(PreviewDevice(rawValue: deviceName))
//            .previewDisplayName(deviceName)
//        }
    }
}
